<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends Base_Controller{

	public function __construct(){
		parent::__construct();
	}
	
	public function index(){
		$this->ci_smarty->display('login.htm');
	}
	
	public function submit(){
		$this->load->library('form_validation');
		$this->form_validation->set_rules('idno', '身份证号', 'required|valid_idcard');
		$this->form_validation->set_rules('password', '密码', 'required');
		if($this->form_validation->run() == false){
			foreach($this->form_validation->error_array() as $filed => $error){
				$this->output_json([
					'retcode' => -1,
					'message' => $error
				]);
			}
		}
		$this->load->model('annuity_user_model');
		$this->load->helper('common');
		$posts = $this->input->post();
		$user = $this->annuity_user_model->get_row([
			'idno' => $posts['idno'],
			'password' => mmd5($posts['password'])
		]);
		if(!$user){
			$this->output_json('身份证号或密码错误');
		}
		$nowtime = time();
		$query = $this->annuity_user_model->set([
			'logintime' => $nowtime
		], $user['id']);
		if($query){
			$user['logintime'] = $nowtime;
			$this->save_login($user);
			$this->output_json('登录成功', 0);
		}
		$this->output_json('登录失败');
	}
	
	// 保存登录信息
	private function save_login($data){
		$this->load->library('session');
		$this->session->set_userdata('annuity_logined', $data);
	}
	
}